package cn.com.greatwall.flowable.rest;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.greatwall.flowable.service.TaskService;
import cn.com.greatwall.flowable.service.dto.TaskDto;
import cn.com.greatwall.flowable.service.dto.criteria.TaskQueryCriteria;
import cn.com.greatwall.logging.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Author herw
 * @Time 2020-11-24 15:26:39
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Flowable：任务管理")
@RequestMapping("/api/flowable/task")
public class TaskController {
    private final TaskService taskService;

    @Log("查询任务")
    @ApiOperation("查询任务")
    @GetMapping
    @PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> query(TaskQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(taskService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询任务")
    @ApiOperation("根据ID查询任务")
    @GetMapping(value = "/{id}")
    @PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> query(@PathVariable String id) {
        return new ResponseEntity<>(taskService.findTaskInfoById(id), HttpStatus.OK);
    }

    @Log("查询我的待办任务")
    @ApiOperation("查询我的待办任务")
    @GetMapping(value = "/todo")
    @PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> queryTodo(TaskQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(taskService.queryTodo(criteria, pageable), HttpStatus.OK);
    }

    @Log("查询我的已办任务")
    @ApiOperation("查询我的已办任务")
    @GetMapping(value = "/done")
    @PreAuthorize("@el.check('task:list')")
    public ResponseEntity<Object> queryDone(TaskQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(taskService.queryDone(criteria, pageable), HttpStatus.OK);
    }

    @Log("认领任务")
    @ApiOperation("认领任务")
    @PutMapping(value = "/claim")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> claim(@RequestBody TaskDto resources) {
        taskService.claimTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("取消认领任务")
    @ApiOperation("取消认领任务")
    @PutMapping(value = "/unclaim")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> unclaim(@RequestBody TaskDto resources) {
        taskService.unclaimTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("完成任务")
    @ApiOperation("完成任务")
    @PutMapping(value = "/complete")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> complete(@RequestBody TaskDto resources) {
        taskService.completeTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("转办任务")
    @ApiOperation("转办任务")
    @PutMapping(value = "/assign")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> assign(@RequestBody TaskDto resources) {
        taskService.assignTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("委派任务")
    @ApiOperation("委派任务")
    @PutMapping(value = "/delegate")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> delegate(@RequestBody TaskDto resources) {
        taskService.delegateTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("执行任务")
    @ApiOperation("执行任务")
    @PutMapping(value = "/execute")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> execute(@RequestParam String taskId) {
        return new ResponseEntity<>(taskService.executeTask(taskId), HttpStatus.OK);
    }
    
    @Log("获取退后节点")
    @ApiOperation("获取退后节点")
    @GetMapping(value = "/backNodes")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> backNodes(@RequestParam String taskId) {
        return new ResponseEntity<>(taskService.getBackNodes(taskId), HttpStatus.OK);
    }
    
    @Log("退回任务")
    @ApiOperation("退回任务")
    @PutMapping(value = "/back")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> back(@RequestBody TaskDto resources) {
        taskService.backTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    
    @Log("终止任务")
    @ApiOperation("终止任务")
    @PutMapping(value = "/stop")
    @PreAuthorize("@el.check('task:edit')")
    public ResponseEntity<Object> stop(@RequestBody TaskDto resources) {
        taskService.stopTask(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
